//
//  SettingTableViewImplement.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2019/5/30.
//  Copyright © 2019 筱鹏. All rights reserved.
//

import UIKit

struct SettingData {
    var name: String
    var action: (IndexPath) -> Void
}

class SettingTableViewImplement: NSObject {

    let cellId: String = "SettingTableViewImplementCellID"
    var sectionDatas: [[SettingData]] = []
    
    var tableView: UITableView!
    weak var viewController: BaseViewController?
    
    convenience init(_ tableView: UITableView) {
        self.init()
        setupData()
        self.tableView = tableView
        setupTableView()
    }
    
    func setupTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
    
    func setupViewController(_ viewController: BaseViewController) {
        self.viewController = viewController
    }
    
    func setupData() {
        let addData = SettingData(name: "添加生日") { [weak self] (indexPath) in
            guard let vc = self?.viewController else { return }
            BirthdayAppendViewController.show(fromVC: vc, birthdayId: nil)
        }
        
        let notiListData = SettingData(name: "通知列表") { [weak self] (indexPath) in
            guard let vc = self?.viewController else { return }
            let lnlistVc = LNListViewController()
            vc.show(lnlistVc, sender: vc)
        }
        
        let alertTimeData = SettingData(name: "提醒时间") { [weak self] (indexPath) in
            guard let vc = self?.viewController else { return }
            let offsetVC = BirthdayTimeOffsetViewController()
            offsetVC.navigationItem.title = "提醒时间"
            vc.show(offsetVC, sender: vc)
        }
        
        let feedbackData = SettingData(name: "意见反馈") { [weak self] (indexPath) in
            guard let vc = self?.viewController else { return }
            let feedbackVC = FeedbackViewController()
            feedbackVC.navigationItem.title = "意见反馈"
            vc.show(feedbackVC, sender: vc)
        }
        
        let dissmissData = SettingData(name: "返回") { [weak self] (indexPath) in
            guard let vc = self?.viewController else { return }
            vc.dismiss(animated: true, completion: nil)
        }
        
        sectionDatas = [[addData, notiListData, alertTimeData], [feedbackData], [dissmissData]]
    }
}

extension SettingTableViewImplement: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionDatas.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionDatas[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.backgroundView?.backgroundColor = Theme.color.background
        cell.contentView.backgroundColor = Theme.color.background
        cell.textLabel?.backgroundColor = Theme.color.background
        cell.textLabel?.textColor = Theme.color.foreground
        cell.textLabel?.textAlignment = .center
        cell.textLabel?.font = Theme.font.date
        
        cell.textLabel?.text = sectionDatas[indexPath.section][indexPath.row].name
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        sectionDatas[indexPath.section][indexPath.row].action(indexPath)
    }
    
}
